home *** CD-ROM | disk | FTP | other *** search
- Program JDBtree_Example;
-
- Uses Crt;
-
- type String_10 = String[10];
- Branch=^Treenode;
- Treenode = record
- Word : String_10;
- count : integer;
- Left,Right : Branch
- end; {TreeNode}
-
- Var Root : Branch;
- Words : Text;
- flag : char; {End of file flag}
-
- {--------------------------------------------------------------------
- BuildTree - Create the ROOT of the tree.
-
- The very first AttachNode in this procedure establishes the ROOT.
- This procedure contains the loop to read until EOF.
-
- --------------------------------------------------------------------}
- Procedure BuildTree(var Words : Text;
- var Root : Branch);
-
- var Nextword : String_10;
- Ancestor : Branch;
-
- {--------------------------------------------------------------------
- AttachNode - Connect New Node to tree.
-
- Create a NEW dynamic variable and set Ancestor to point to it.
- Word becomes equal to Nextword.
- Count is set to 1. This is the first occurance of the word.
- Left and Right are set to nil.
-
- NOTE: This Procedure is only called for two reasons -
-
- 1. It is establishing the ROOT.
- 2. If Ancestor is nil during PutInTree.
-
- --------------------------------------------------------------------}
- Procedure AttachNode(Nextword :String_10;
- Var Ancestor : Branch);
- begin {AttachNode}
- New(Ancestor);
- with Ancestor^ do
- begin
- word := Nextword;
- Count := 1;
- Left := nil; Right := nil
- end
- end; {AttachNode}
- {--------------------------------------------------------------------
- PutInTree - Process a Record.
-
- This routine decides where in the tree the node belongs.
- Notice the comparison statements.
-
- --------------------------------------------------------------------}
- Procedure PutInTree (Nextword : String_10;
- var Ancestor : Branch);
-
- begin {PutInTree}
- if Ancestor = nil then
- AttachNode(Nextword, Ancestor)
- else if Nextword = Ancestor^.word then
- Ancestor^.count := Ancestor^.count +1
- else if Nextword < Ancestor^.word then
- PutInTree(Nextword, Ancestor^.Left)
- else PutInTree(Nextword, Ancestor^.Right)
- end; {PutInTree}
-
- begin {BuildTree}
- assign(Words, 'WORDS.DAT');
- reset(Words);
- Readln(Words, Nextword);
- AttachNode(Nextword, Root);
- while not EOF(Words) do
- begin
- readln(Words, Nextword);
- Ancestor := Root;
- PutInTree(Nextword, Ancestor);
- end;
- Close(Words);
- end; {BuildTree}
-
- Procedure Traverse(Root : Branch);
-
- begin {Traverse}
- if Root <> nil then
- begin
- Traverse(Root^.Left);
- Write(Root^.Word,' ');
- Writeln(Root^.Count);
- Traverse(Root^.Right);
- end;
- end; {Traverse}
-
- Procedure BBS_AD;
- const filler=(' ');
-
- begin {BBS_AD}
- Clrscr;
- Writeln(filler,' If you like this');
- Writeln;
- Writeln(filler,' - Call -');
- Writeln;
- Writeln(filler,' Polysyncronism BBS (312) 358-5104');
- Writeln(filler,'8 Data Bits - Parity None - 1 Stop Bit');
- Writeln(filler,' 100 Megabytes of online storage');
- Writeln(filler,' 300:1200:2400 baud');
- Writeln(filler,' Open 24hrs');
- Writeln(filler,' Sysop: Jeff Darling');
- Writeln(filler,' Especially for Programmers.');
- end; {BBS_AD}
-
- begin {MAIN}
- Flag := 'Y';
- While Upcase(Flag) <> 'N' do
- begin
- ClrScr;
- BuildTree(Words,Root);
- Writeln('Word':1,'Ocurrences':15);
- Writeln('~~~~ ~~~~~~~~~~');
- Traverse(root);
- Writeln;
- Write('Run this program again? (y/n) ');readln(flag);
- end;
- BBS_Ad;
- end. {MAIN}
-